﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using BrnMall.Business;
using BrnMall.Manager.Domain.WeApp;
using BrnMall.Manager.Manager.WeApp;
using BrnMall.Sdk.WeiXin;
using BrnMall.Sdk.WeiXin.weapp;
using BrnMall.Services;
using BrnMall.Web.Framework;
using BrnMall.Web.StoreAdmin.Models;
using BrnMall.Web.StoreAdmin.Util;
using BrnMall.WeiXin.Sdk;
using Newtonsoft.Json;

namespace BrnMall.Web.StoreAdmin.controllers
{
    /// <summary>
    /// 小程序定制
    /// </summary>
    public class WeAppController : BaseStoreAdminController
    {
        /// <summary>
        /// 预览
        /// </summary>
        /// <returns></returns>
        public ActionResult Preview()
        {
            return View();
        }

        public ActionResult StoreAppList()
        {
            var model = StoreApplicationManager.GetStoreApplications(WorkContext.StoreId);
            MallUtils.SetAdminRefererCookie(
                $"{Url.Action("StoreAppList")}");
            return View(model);
        }
        /// <summary>
        /// 第三方调用
        /// </summary>
        /// <param name="storeAppId"></param>
        /// <returns></returns>
        public ActionResult ApplicationBind(int storeAppId)
        {
            return View(storeAppId);
            //return RedirectToAction("ApplicationBind", "WeChatNotify", new {storeAppId = storeAppId});
        }
        /// <summary>
        /// 设置底部导航
        /// </summary>
        /// <param name="storeAppId"></param>
        /// <returns></returns>
        public ActionResult StoreAppTabBarSetting(int storeAppId)
        {
            var storeApp = StoreApplicationManager.GetStoreApplication(storeAppId);
            var tabbarJson = storeApp.UserTabBar;
            if (string.IsNullOrEmpty(storeApp.UserTabBar))
            {
                tabbarJson = WeApplicationManager.GetWeApplication(storeApp.AppId).RawTabBar;
                var rawTabBar= JsonConvert.DeserializeObject<StoreAppTabBarSettingModel>(tabbarJson);
                rawTabBar.backgroundColor = "#fff";
                rawTabBar.borderStyle = "white";
                rawTabBar.boxShadow = "0 0 6px 0";
                rawTabBar.color = "#6e6d6b";
                rawTabBar.selectedColor = "#e64340";
                var defaultTabbarJson = JsonConvert.SerializeObject(rawTabBar);
                storeApp.UserTabBar = defaultTabbarJson;
                tabbarJson = defaultTabbarJson;
                StoreApplicationManager.UpdateStoreApplication(storeApp);
            }
                
            var tabBar = JsonConvert.DeserializeObject<StoreAppTabBarSettingModel>(tabbarJson);
            if (tabBar == null)
                return PromptView("错误的配置文件");
            var rawPages =
                JsonConvert.DeserializeObject<IList<string>>(WeApplicationManager.GetWeApplication(storeApp.AppId)
                    .RawPages);
            tabBar.AppPages = tabBar.AppPages.Where(t => rawPages.Contains((t.Url).ToString().Split('?')[0])).ToList();


            tabBar.StoreAppId = storeAppId;
            MallUtils.SetAdminRefererCookie(
                $"{Url.Action("StoreAppTabBarSetting")}?storeAppId={storeAppId}");
            return View(tabBar);
        }
        [HttpPost]
        public ActionResult EditStoreAppTabBarSetting(StoreAppTabBarSettingModel model)
        {
            var storeApp = StoreApplicationManager.GetStoreApplication(model.StoreAppId);
            var tabBarPages = (TabBarPages)model;
            
            if(tabBarPages.list.Count==0)
                return PromptView("保存设置出错！");

            foreach (var barPage in tabBarPages.list)
            {
                //小程序模板里面的文件命名错误，导致这里需要特殊处理
                if (barPage.iconPath.Equals("tabicon/fenxiao-off-1.png", StringComparison.InvariantCultureIgnoreCase))
                {
                    barPage.iconPath = "tabicon/fenxiao-off-1.png.png";
                }
            }
            storeApp.UserTabBar = JsonConvert.SerializeObject(tabBarPages);
            StoreApplicationManager.UpdateStoreApplication(storeApp);
            return PromptView("保存成功！");

        }

        public ActionResult CommitTabBar(StoreAppTabBarSettingModel model)
        {
            
            var storeApp = StoreApplicationManager.GetStoreApplication(model.StoreAppId);
            if (storeApp.DevStatus != 5) return PromptView("未发布的小程序不能修改底部导航！");
            StoreAppReleaseManager.UpgradeStoreApp(model.StoreAppId);
            MallUtils.SetAdminRefererCookie(
                $"{Url.Action("StoreAppList")}");
            return PromptView("提交成功，请等待微信审核！");
        }

        public ActionResult ResetTabBar(int storeAppId)
        {
            var storeApp = StoreApplicationManager.GetStoreApplication(storeAppId);
            if (storeApp.DevStatus != 5) return PromptView("未发布的小程序不能修改底部导航！");
            storeApp.UserTabBar = WeApplicationManager.GetWeApplication(storeApp.AppId).RawTabBar;
            StoreApplicationManager.UpdateStoreApplication(storeApp);
            //StoreAppReleaseManager.UpgradeStoreApp(storeAppId);
            //MallUtils.SetAdminRefererCookie(
            //    $"{Url.Action("StoreAppList")}");
            return PromptView("重置成功！");
        }
        public ActionResult DeleteTabBar(int storeAppId,int tabIndex)
        {
            
            var storeApp = StoreApplicationManager.GetStoreApplication(storeAppId);
            
            var tabBar = JsonConvert.DeserializeObject<StoreAppTabBarSettingModel>(storeApp.UserTabBar);
            if (tabBar.list.Count <= 2)
            {
                return PromptView("底部导航最少2个，最多5个！");
            }
            tabBar.list.RemoveAt(tabIndex);
            storeApp.UserTabBar = JsonConvert.SerializeObject(tabBar);
            StoreApplicationManager.UpdateStoreApplication(storeApp);
            return PromptView("删除成功！");
        }
        public ActionResult NewTabBar(int storeAppId)
        {

            var storeApp = StoreApplicationManager.GetStoreApplication(storeAppId);

            var tabBar = JsonConvert.DeserializeObject<StoreAppTabBarSettingModel>(storeApp.UserTabBar);
            if (tabBar.list.Count == 5)
            {
                return PromptView("底部导航最少2个，最多5个！");
            }
            tabBar.list.Add(tabBar.list.Last());
            storeApp.UserTabBar = JsonConvert.SerializeObject(tabBar);
            StoreApplicationManager.UpdateStoreApplication(storeApp);
            return PromptView("新增成功！");
        }
        #region 页面配置

        public ActionResult StoreAppPageSetting()
        {
            throw new NotImplementedException();
        }

        #endregion

    }
}
